﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UpdatedHomeMaker
{
    public partial class MainForm : Form
    {

        private ServicingList clients;
        private ClientInfo currentClient;
        private DocumentedGroup currentGroup;
        public MainForm()
        {
            InitializeComponent();
            clients = new ServicingList();
            try
            {
                clients = ServicingList.DeserializeFromXML();
            }
            catch
            {
                //exception eater NOM NOM NOM
            }
            refreshClientCombo();
        }
        private void refreshClientCombo()
        {
            lblID.Text = "ID = ";
            lblTotalUnits.Text = "Total Units = ";
            lblUsedUnits.Text = "Used Units = ";
            cmboClients.Text = "";
            cmboClients.Items.Clear();
            foreach (ClientInfo c in clients.Clients)
            {
                cmboClients.Items.Add(c);
            }
            if (cmboClients.Items.Count != 0)
                cmboClients.SelectedIndex = cmboClients.Items.Count - 1;
        }

        private void btnAddClient_Click(object sender, EventArgs e)
        {
            using (AddClientForm form = new AddClientForm())
                if (form.ShowDialog() == DialogResult.OK)
                    clients.Clients.Add(form.newClient);
            refreshClientCombo();
        }

        private void btnRemoveClient_Click(object sender, EventArgs e)
        {
            if (cmboClients.SelectedItem != null)
                clients.Clients.RemoveAt(clients.Clients.IndexOf((ClientInfo)cmboClients.SelectedItem));
            refreshClientCombo();
            refreshUnits();
            setComboInfo();
        }

        private void btnEditClient_Click(object sender, EventArgs e)
        {
            using (AddClientForm form = new AddClientForm(clients.Clients[clients.Clients.IndexOf((ClientInfo)cmboClients.SelectedItem)]))
                if (form.ShowDialog() == DialogResult.OK)
                {
                    clients.Clients.RemoveAt(clients.Clients.IndexOf((ClientInfo)cmboClients.SelectedItem));
                    clients.Clients.Add(form.newClient);
                }
            refreshClientCombo();
        }

        private void cmboClients_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmboClients.SelectedItem != null)
            {
                currentClient = (ClientInfo)cmboClients.SelectedItem;
                refreshUnits();
                lblID.Text = "ID = " + currentClient.ID1;
                lblTotalUnits.Text = "Total Units = " + currentClient.TotalUnits;
                lblUsedUnits.Text = "Used Units = " + currentClient.UsedUnits;
                lblRemainingUnits.Text = "Remaining Units = " + (currentClient.TotalUnits - currentClient.UsedUnits);
                setComboInfo();
            }
            else
            {
                lblID.Text = "ID = ";
                lblTotalUnits.Text = "Total Units = ";
                lblUsedUnits.Text = "Used Units = ";
                lblRemainingUnits.Text = "Remaining Units = ";
            }
        }

        private void setComboInfo()
        {
            comboSets.Text = "";
            comboSets.Items.Clear();
            flowDays.Controls.Clear();
            foreach (DocumentedGroup grouping in currentClient.groupInfo)
                comboSets.Items.Add(grouping);
            if (comboSets.Items.Count > 0)
                comboSets.SelectedIndex = comboSets.Items.Count - 1;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                ServicingList.SerializeToXML(clients);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddGroup_Click(object sender, EventArgs e)
        {
            saveGroup();
            using (AddGroup form = new AddGroup())
                if (form.ShowDialog() == DialogResult.OK)
                    currentClient.groupInfo.Add(new DocumentedGroup(form.txtName.Text));
            refreshComboSets();
            comboSets.SelectedIndex = comboSets.Items.Count - 1;
        }

        private void comboSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            flowDays.Controls.Clear();
            if (comboSets.SelectedItem != null)
            {
                currentGroup = (DocumentedGroup)comboSets.SelectedItem;
                foreach (DocumentedDay day in currentGroup.daysInGroup)
                    flowDays.Controls.Add(new DayThing((DocumentedDay)day));
            }
        }
        private void refreshComboSets()
        {
            comboSets.Items.Clear();
            comboSets.Text = "";
            foreach (DocumentedGroup group in currentClient.groupInfo)
                comboSets.Items.Add(group);
            if (currentClient.groupInfo.Count != 0)
                comboSets.SelectedIndex = 0;
            else
                flowDays.Controls.Clear();
            saveGroup();
        }

        private void btnAddDay_Click(object sender, EventArgs e)
        {
            if (comboSets.SelectedItem != null)
            {
                flowDays.Controls.Add(new DayThing());
                using (DayInformation form = new DayInformation())
                {
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.OK)
                    {
                        flowDays.Controls.OfType<DayThing>().Last<DayThing>().DayInfo = form.dayInfo;
                    }
                }
            }
            saveGroup();
        }

        private void btnDeleteDay_Click(object sender, EventArgs e)
        {
            try
            {
                flowDays.Controls.Remove(flowDays.Controls.OfType<DayThing>().Last<DayThing>());
                saveGroup();
            }
            catch
            {

            }
        }

        private void saveGroup()
        {
            if (currentGroup != null)
            {
                try
                {
                    currentGroup.TotalUnits = 0;
                    currentGroup.daysInGroup.Clear();
                    foreach (DayThing day in flowDays.Controls.OfType<DayThing>())
                    {
                        currentGroup.daysInGroup.Add(day.DayInfo);
                        currentGroup.TotalUnits += day.DayInfo.Units;
                    }
                    currentClient.groupInfo[comboSets.SelectedIndex] = currentGroup;
                    clients.Clients[cmboClients.SelectedIndex] = currentClient;
                    refreshUnits();
                    ServicingList.SerializeToXML(clients);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Something went wrong while saving group, " + ex.Message);
                }
            }
        }
        private void btnDeleteSet_Click(object sender, EventArgs e)
        {
            if (comboSets.SelectedItem != null)
            {
                currentClient.groupInfo.RemoveAt(comboSets.SelectedIndex);
                refreshComboSets();
            }
        }
        private void refreshUnits()
        {
            currentClient.UsedUnits = 0;
            foreach (DocumentedGroup group in currentClient.groupInfo)
            {
                currentClient.UsedUnits += group.TotalUnits;
            }
            lblUsedUnits.Text = "Used Units = " + currentClient.UsedUnits;
            lblRemainingUnits.Text = "Remaining Units = " + (currentClient.TotalUnits - currentClient.UsedUnits);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            saveGroup();
        }
        
    }
}
